package sketchpad.business.function.impl;

import sketchpad.gui.drawinggraph.Group;
import sketchpad.gui.control.DrawControl;
import sketchpad.gui.drawinggraph.DrawItem;
import sketchpad.business.function.ComposeService;

import java.util.List;
import java.util.Map;

public final class ComposeServiceImpl implements ComposeService {

    private DrawControl drawControl;

    public ComposeServiceImpl(DrawControl control){drawControl = control;}

    @Override
    public void compose() {
        if (drawControl.isSelect() && drawControl.getSelectedItemList().size()>1){
            List<Integer> list = drawControl.getSelectedItemList();
            Group group = new Group(drawControl.getGlobalId());
            Map<Integer, DrawItem> map = drawControl.getDrawItemMap();
            for (int id : list){
                DrawItem item = map.get(id);
                item.setGroupId(group.getId());
                group.add(item);
            }
            drawControl.getGroupMap().put(group.getId(),group);
        }
    }

    @Override
    public void decompose() {
        if (drawControl.isSelect() && drawControl.getGroupMap().size()>0){
            List<Integer> list = drawControl.getSelectedItemList();
            Map<Integer, DrawItem> map = drawControl.getDrawItemMap();
            Map<Integer, Group> groupMap =drawControl.getGroupMap();
            for (int i : list){
                Group group = groupMap.get(map.get(i).getGroupId());
                if (group==null)continue;
                groupMap.remove(group.getId());
                group.dismiss();
            }
        }
    }
}
